package com.echovin.esb.gateway.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.echovin.esb.gateway.dto.*;
import com.echovin.esb.gateway.mapper.*;
import com.echovin.esb.gateway.model.*;
import com.echovin.esb.gateway.service.GatewayRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

@Service
@Transactional
public class GatewayRegisterServiceImpl implements GatewayRegisterService {
    @Autowired
    GatewayServiceGroupMapper groupMapper;
    @Autowired
    GatewayServiceMapper serviceMapper;
    @Autowired
    GatewayServiceActionMapper actionMapper;
    @Autowired
    GatewayServiceWebserviceMapper webserviceMapper;
    @Autowired
    GatewayIPLimitMapper ipLimitMapper;
    @Autowired
    GatewayParameterValidateMapper parameterValidateMapper;
    @Autowired
    GatewayErrorResponseMapper errorResponseMapper;

    @Override
    public int insert(GatewayRegister record) {
        GatewayServerGroupDto serverGroupDto = record.getServerGroup();
        GatewayServerGroup serverGroup = new GatewayServerGroup();
        serverGroup.setGroupName(serverGroupDto.getGroupName());
        serverGroup.setRemark(serverGroupDto.getRemark());
        int result = groupMapper.insert(serverGroup);
        int result1 = 0;
        int result2 = 0;
        int result3 = 0;
        int result4 = 0;
        int result5 = 0;
        int result6 = 0;
        int result7 = 0;
        List<GatewayServerDto> serverDtos = serverGroupDto.getServerDtos();
        Iterator<GatewayServerDto> iterator = serverDtos.iterator();
        while (iterator.hasNext()) {
            GatewayServerDto serverDto = iterator.next();
            GatewayServer server = new GatewayServer();
            server.setGroupId(serverGroup.getId());
            server.setPath(serverDto.getPath());
            server.setServiceName(serverDto.getServiceName());
            server.setProtocol(serverDto.getProtocol());
            server.setRouteRule(serverDto.getRouteRule());
            server.setRemark(serverDto.getRemark());
            result1 = serviceMapper.insert(server);
            GatewayServerWebserviceDto webserviceDto = serverDto.getWebserviceDto();
            GatewayServerWebservice webservice = new GatewayServerWebservice();
            webservice.setServiceId(server.getId());
            webservice.setWsdl(webserviceDto.getWsdl());
            result2 = webserviceMapper.insert(webservice);
            GatewayIPLimitApiDto ipLimitApiDto = serverDto.getIpLimitApiDto();
            GatewayIPLimitApi ipLimitApi = new GatewayIPLimitApi();
            ipLimitApi.setServiceId(server.getId());
            ipLimitApi.setPolicyType(ipLimitApiDto.getPolicyType());
            ipLimitApi.setIpAddress(ipLimitApiDto.getIpAddress());
            result3 = ipLimitMapper.insert(ipLimitApi);
            if (serverDto.getErrorResponseDto() != null) {
                GatewayErrorResponseDto errorResponseDto = serverDto.getErrorResponseDto();
                GatewayErrorResponse errorResponse = new GatewayErrorResponse();
                errorResponse.setServiceId(server.getId());
                errorResponse.setErrorCode(errorResponseDto.getErrorCode());
                errorResponse.setErrorResponse(errorResponseDto.getErrorResponse());
                result4 = errorResponseMapper.insert(errorResponse);
            } else {
                result4 = 1;
            }
            List<GatewayServerActionDto> serverActionDtos = serverDto.getActionDtos();
            Iterator<GatewayServerActionDto> iterator1 = serverActionDtos.iterator();
            while (iterator1.hasNext()) {
                GatewayServerActionDto actionDto = iterator1.next();
                GatewayServerAction action = new GatewayServerAction();
                action.setServiceId(server.getId());
                action.setActionName(actionDto.getActionName());
                result5 = actionMapper.insert(action);
                GatewayParameterSchemaDto parameterSchemaDto = actionDto.getParameterSchemaDto();
                GatewayParameterSchema parameterSchema = new GatewayParameterSchema();
                parameterSchema.setActionId(action.getId());
                parameterSchema.setCheckType(parameterSchemaDto.getCheckType());
                parameterSchema.setSchema(parameterSchemaDto.getSchema());
                result6 = parameterValidateMapper.insert(parameterSchema);
                GatewayErrorResponseDto errorResponseDto = actionDto.getErrorResponseDto();
                GatewayErrorResponse errorResponse = new GatewayErrorResponse();
                errorResponse.setActionId(action.getId());
                errorResponse.setErrorCode(errorResponseDto.getErrorCode());
                errorResponse.setErrorResponse(errorResponseDto.getErrorResponse());
                result7 = errorResponseMapper.insert(errorResponse);
            }
        }
        return (result>0 && result1>0 && result2>0 && result3>0 && result4>0 && result5>0 && result6>0 && result7>0) ? 1 : 0;
    }

    @Override
    public int delete(GatewayRegister record) {
        GatewayServerGroupDto serverGroupDto = record.getServerGroup();
        GatewayServerGroup serverGroup = new GatewayServerGroup();
        serverGroup.setId(serverGroupDto.getId());
        serverGroup.setUpdater(serverGroupDto.getUpdater());
        serverGroup.setUpdateTime(serverGroupDto.getUpdateTime());
        int result = groupMapper.delete(serverGroup);
        return result;
    }
//        GatewayServerGroupDto serverGroupDto = record.getServerGroup();
//        GatewayServerGroup serverGroup = new GatewayServerGroup();
//        serverGroup.setId(serverGroupDto.getId());
//        serverGroup.setUpdater(serverGroupDto.getUpdater());
//        serverGroup.setUpdateTime(serverGroupDto.getUpdateTime());
//        int result = groupMapper.delete(serverGroup);
//        int result1 = 0;
//        int result2 = 0;
//        int result3 = 0;
//        int result4 = 0;
//        int result5 = 0;
//        int result6 = 0;
//        int result7 = 0;
//        List<GatewayServerDto> serverDtos = serverGroupDto.getServerDtos();
//        Iterator<GatewayServerDto> iterator = serverDtos.iterator();
//        while (iterator.hasNext()) {
//            GatewayServerDto serverDto = iterator.next();
//            GatewayServer server = new GatewayServer();
//            server.setId(serverDto.getId());
//            server.setUpdater(serverDto.getUpdater());
//            server.setUpdateTime(serverDto.getUpdateTime());
//            result1 = serviceMapper.delete(server);
//            GatewayServerWebserviceDto webserviceDto = serverDto.getWebserviceDto();
//            GatewayServerWebservice webservice = new GatewayServerWebservice();
//            webservice.setServiceId(webserviceDto.getId());
//            webservice.setUpdater(webserviceDto.getUpdater());
//            webservice.setUpdateTime(webserviceDto.getUpdateTime());
//            result2 = webserviceMapper.delete(webservice);
//            GatewayIPLimitApiDto ipLimitApiDto = serverDto.getIpLimitApiDto();
//            GatewayIPLimitApi ipLimitApi = new GatewayIPLimitApi();
//            ipLimitApi.setId(ipLimitApiDto.getId());
//            ipLimitApi.setUpdater(ipLimitApiDto.getUpdater());
//            ipLimitApi.setUpdateTime(ipLimitApiDto.getUpdateTime());
//            result3 = ipLimitMapper.delete(ipLimitApi);
//            if (serverDto.getErrorResponseDto() != null) {
//                GatewayErrorResponseDto errorResponseDto = serverDto.getErrorResponseDto();
//                GatewayErrorResponse errorResponse = new GatewayErrorResponse();
//                errorResponse.setId(errorResponseDto.getId());
//                errorResponse.setUpdater(errorResponseDto.getUpdater());
//                errorResponse.setUpdateTime(errorResponseDto.getUpdateTime());
//                result4 = errorResponseMapper.delete(errorResponse);
//            } else {
//                result4 = 1;
//            }
//            List<GatewayServerActionDto> serverActionDtos = serverDto.getActionDtos();
//            Iterator<GatewayServerActionDto> iterator1 = serverActionDtos.iterator();
//            while (iterator1.hasNext()) {
//                GatewayServerActionDto actionDto = iterator1.next();
//                GatewayServerAction action = new GatewayServerAction();
//                action.setId(actionDto.getId());
//                action.setUpdater(actionDto.getUpdater());
//                action.setUpdateTime(actionDto.getUpdateTime());
//                result5 = actionMapper.delete(action);
//                GatewayParameterSchemaDto parameterSchemaDto = actionDto.getParameterSchemaDto();
//                GatewayParameterSchema parameterSchema = new GatewayParameterSchema();
//                parameterSchema.setId(parameterSchemaDto.getId());
//                parameterSchema.setUpdater(parameterSchemaDto.getUpdater());
//                parameterSchema.setUpdateTime(parameterSchemaDto.getUpdateTime());
//                result6 = parameterValidateMapper.delete(parameterSchema);
//                GatewayErrorResponseDto errorResponseDto = actionDto.getErrorResponseDto();
//                GatewayErrorResponse errorResponse = new GatewayErrorResponse();
//                errorResponse.setId(errorResponseDto.getId());
//                errorResponse.setUpdater(errorResponseDto.getUpdater());
//                errorResponse.setUpdateTime(errorResponseDto.getUpdateTime());
//                result7 = errorResponseMapper.delete(errorResponse);
//            }
//        }
//        return (result>0 && result1>0 && result2>0 && result3>0 && result4>0 && result5>0 && result6>0 && result7>0) ? 1 : 0;
//    }
}
